// packageE/appointment/client/evaluate/evaluate.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    autosize: { minHeight: 240 },
    imagesList: [],
    order_info: {
      has_one_order_project: {},
      has_one_worker: {},
      has_one_store: {},
    },
    comment: {},
    photosize: 1024 * 1024 * 4,
    imgIndex: 1, //相册图片预览起始位置
    show: false, //图片预览
    applyModel: {
      order_service_id: 0,
      content: "",
      store_level: 5,
      project_level: 5,
      worker_level: 5,
      images: [],
    },
    applyMoreModel: {
      content: "",
      images: [],
    },
    appointmentLang: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.applyModel.order_service_id = options.service_id;
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.comment.get-comment-by-order-service-id"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        order_service_id: this.data.applyModel.order_service_id,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);
        this.setData({
          order_info: res.data.order_info,
          comment: res.data.comment,
        });

        if (!this.data.comment) {
          this.setData({
            comment: {},
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  changeProjectLevel(e) {
    // this.setData({
    // 	'applyModel.project_level':e.detail
    // })
    this.data.applyModel.project_level = e.detail;
  },
  changeWorkerLevel(e) {
    // this.setData({
    // 	'applyModel.worker_level':e.detail
    // })
    this.data.applyModel.worker_level = e.detail;
  },
  changeStoreLevel(e) {
    // this.setData({
    // 	'applyModel.store_level':e.detail
    // })
    this.data.applyModel.store_level = e.detail;
  },
  changeContent(e) {
    this.data.applyModel.content = e.detail.value;
  },
  changeMoreContent(e) {
    this.data.applyMoreModel.content = e.detail.value;
  },
  makeEvaluate() {
    this.data.applyModel.images = JSON.stringify(this.data.imagesList);
    console.log(this.data.applyModel);
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.comment.create-comment"
    );
    app._postNetWork({
      url: urlStr,
      data: this.data.applyModel,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);
        this.tips(res.msg);
        wx.navigateBack();
      },
    });
  },
  makeMoreEvaluate() {
    this.data.applyMoreModel.id = this.data.comment.id;
    this.data.applyMoreModel.images = JSON.stringify(this.data.imagesList);
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.comment.append-comment"
    );
    app._postNetWork({
      url: urlStr,
      data: this.data.applyMoreModel,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);
        this.tips(res.msg);
        wx.navigateBack();
      },
    });
  },

  async bindUpImage() {
    let fileArray = await this.selectImage();
    console.log(fileArray);
    let imagesList = this.data.imagesList;
    for (let i = 0; i < fileArray.length; i++) {
      let data = await this.uploadImage(fileArray[i]);
      imagesList.push(data.img_url);
      this.setData({
        imagesList,
      });
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  selectImage() {
    return new Promise((resolve, reject) => {
      wx.chooseImage({
        sizeType: ["original", "compressed"],
        sourceType: ["album", "camera"],
        success(res) {
          resolve(res.tempFilePaths);
        },
      });
    });
  },
  uploadImage(tempFile) {
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: "上传图片中",
      });
      let urlStr = app.getNetAddresss("upload.uploadPic");
      wx.uploadFile({
        url: urlStr,
        filePath: tempFile,
        name: "file",
        success(resdata) {
          var res = JSON.parse(resdata.data);
          if (res.result == 1) {
            resolve(res.data);
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        complete: () => {
          wx.hideLoading();
        },
      });
    });
  },
  tips(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
      duration: 2000,
    });
    return false;
  },
});
